<template>
  <div class="serach-artist">
    <div class="artistCount">找到{{artistCount}}位歌手</div>
    <div class="artists-list">
      <div @click.stop="$router.push({ name: 'artistAccount', query: { id:item.id } });" v-for="(item,index) of artists"
        :key="index+'artist'" class="artist">
        <img class="img" :src="item.picUrl+'?param=70y70'" alt="">
        <div class="name">{{item.name}}</div>
        <div class="alias">{{getalias(item)}}</div>
        <div v-show="getAccount(item)" class="iconbg"><span class="el-icon-user"></span></div>
      </div>
    </div>
    <div v-show="artistCount >=30" class="pageset">
      <el-pagination :page-size="30" :current-page.sync="curPage" @current-change="getsearchArtist()"
        layout="prev, pager, next" :total="artistCount" background></el-pagination>
    </div>
  </div>
</template>
<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import { getsearchAPI } from "@/api/search";
export default {
  name: "serach-artist",
  //import引入的组件需要注入到对象中才能使用
  components: {},
  props: {
    searchText: {
      require: true,
    },
  },
  data() {
    //这里存放数据
    return {
      artists: {},
      artistCount: 0,
      limit: 0,
      curPage: 1,
      pageSize: 30,
    };
  },
  //计算属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {
    searchText() {
      this.getsearchArtist();
    },
  },
  //方法集合
  methods: {
    // 判断是否有别名
    getalias(artist) {
      if (artist.alias[0] != undefined) {
        return "(" + artist.alias[0] + ")";
      } else {
        return "";
      }
    },
    getAccount(artist) {
      if (artist.accountId != undefined) {
        return true;
      } else {
        return false;
      }
    },
    async getsearchArtist() {
      try {
        this.artists = [];
        const { data } = await getsearchAPI(
          this.searchText,
          30,
          (this.curPage - 1) * 30,
          100
        );
        // console.log(data.result);
        this.artists = data.result.artists;
        this.artistCount = data.result.artistCount;
        // 页面滚动优化用户体验
        if (document.documentElement.scrollTop != 0) {
          document.documentElement.scrollTop = 0;
        }
      } catch (error) {
        console.log(error + "搜索歌手失败");
      }
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.getsearchArtist();
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style scoped lang='less'>
.serach-artist {
  margin: 0 auto;
  padding-bottom: 200px;
  .artists-list {
    display: flex;
    flex-wrap: wrap;
    .artist {
      position: relative;
      flex-shrink: 0;
      width: 1200px;
      display: flex;
      align-items: center;
      cursor: pointer;
      .img {
        margin: 10px 10px 10px 10px;
        height: 70px;
        width: 70px;
        border-radius: 5px;
      }
      .name {
        color: rgb(68, 68, 68);
      }
      .alias {
        color: rgb(167, 167, 167);
        margin-left: 10px;
      }
      .iconbg {
        position: absolute;
        right: 30px;
        width: 22px;
        height: 22px;
        background-color: rgb(252, 47, 47);
        border-radius: 20px;

        .el-icon-user {
          padding-left: 4px;
          color: white;
        }
      }
    }
    .artist:nth-child(even) {
      background-color: #fafafa;
    }
    .artist:hover {
      background-color: #f1f2f3;
    }
  }

  .pageset {
    display: flex;
    justify-content: center;
    .el-pagination {
      margin-top: 50px;
      margin-bottom: 140px;
    }
  }
}
</style>